package orion.cricScoreCard.Activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class CricScoreCardActivity extends Activity implements OnClickListener {

	private EditText editTeam1;
	private EditText editTeam2;
	private EditText editOver;
	private Button btnToss;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		showOptions();
		init();
		btnToss.setOnClickListener(this);
	}

	private void init() {
		editTeam1 = (EditText) findViewById(R.id.edittxtTeam1);
		editTeam2 = (EditText) findViewById(R.id.edittxtTeam2);
		editOver = (EditText) findViewById(R.id.edittxtOvers);
		btnToss = (Button) findViewById(R.id.btnToss);
	}

	private void showOptions() {
		new AlertDialog.Builder(this)
				.setIcon(R.drawable.ic_launcher)
				.setTitle("CricScore")
				.setMessage("Select One")
				.setPositiveButton("History",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						})
				.setNegativeButton("New Game",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						}).show();

	}

	private void showToss(final String opt1, final String opt2,
			final String overs) {
		new AlertDialog.Builder(this)
				.setIcon(R.drawable.ic_launcher)
				.setTitle("CricScore")
				.setMessage("Batting")
				.setPositiveButton(opt1, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent playerInt = new Intent(getApplicationContext(),
								TeamDetails.class);
						Bundle data = new Bundle();
						data.putString("Batting", opt1);
						data.putString("Bowling", opt2);
						data.putString("Overs", overs);
						playerInt.putExtras(data);
						startActivity(playerInt);
					}
				})
				.setNegativeButton(opt2, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent playerInt = new Intent(getApplicationContext(),
								TeamDetails.class);
						Bundle data = new Bundle();
						data.putString("Batting", opt2);
						data.putString("Bowling", opt1);
						data.putString("Overs", overs);
						playerInt.putExtras(data);
						startActivity(playerInt);
					}
				}).show();

	}

	@Override
	public void onClick(View v) {
		String overs = editOver.getText().toString();
		String team1 = editTeam1.getText().toString();
		String team2 = editTeam2.getText().toString();
		switch (v.getId()) {
		case R.id.btnToss:
			showToss(team1, team2, overs);
			break;
		}

	}

}